package com.cens.mall.commodity.web;

import com.cens.mall.commodity.entity.CategoryEntity;
import com.cens.mall.commodity.service.CategoryService;
import com.cens.mall.commodity.vo.Catalog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redisson;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @ResponseBody
    @GetMapping("index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatalogJson(){
        return categoryService.getCatalogJson();
    }

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> categoryEntityList = categoryService.getLevelOneCategorys();
        model.addAttribute("categorys",categoryEntityList);
        //视图解析器
        //prefix: classpath:/templates/
        //suffix: .html
        return "index";
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        RLock lock = redisson.getLock("lock");
        lock.lock();
        //阻塞式等待。默认加锁就是30s
        //锁的自动续期，如果业务超长，运行期间会自动给锁续上新的30s。不用担心业务时间长，锁自动过期被删掉的问题
        //加锁的业务只要运行完成，就不会给当前锁续期，即时不手动解锁，锁也会默认在30s以后自动删除
        try {
            System.out.println("hello");
        } finally {
            lock.unlock();
        }
        return "hello";
    }

    @ResponseBody
    @GetMapping("/write")
    public String writeValue(){
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        try {
            rLock.lock();
            String v = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set("v",v);
            return v;
        }finally {
            rLock.unlock();
        }
    }

    @ResponseBody
    @GetMapping("/read")
    public String readValue(){
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        try {
            rLock.lock();
            return redisTemplate.opsForValue().get("v");
        }finally {
            rLock.unlock();
        }
    }

    @ResponseBody
    @GetMapping("/park")
    public String park() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        park.trySetPermits(20);
        //获取一个信号。阻塞式方法
        park.acquire();
        return "ok";
    }

    @ResponseBody
    @GetMapping("/go")
    public String go() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        //释放一个信号量
        park.release();
        return "ok";
    }

    @ResponseBody
    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        RCountDownLatch countDownLatch = redisson.getCountDownLatch("door");
        countDownLatch.trySetCount(7);
        countDownLatch.await();
        return "lockDoor";
    }


    @ResponseBody
    @GetMapping("/goDoor")
    public String goDoor(){
        RCountDownLatch countDownLatch = redisson.getCountDownLatch("door");
        countDownLatch.countDown();
        return "goDoor";
    }


}
